<?php

namespace app\index\controller;

use think\Controller;

class Safecenter extends Base {
	function getcode() {
		$type = $this->request->param ( 'type' );
		$account = $type == 'email' ? $this->request->param ( 'email' ) : $this->member ['email'];
		if ($account == '')
			echo json_encode ( array ('code' => 98,'msg' => lang ( 'email_auth' ) ) );
		else {
			$code = model ( 'Verifycode' )->getCode ( array ('type' => 'email','account' => $account ) );
			$content = lang ( 'sms_code_tips' ) . $code;
			if ($code != '') {
				$title = lang ( 'verify_code' );
				$log = model ( 'Phpmail' )->sendMail ( $account, $title, $content );
				if ($log == 'success')
					echo json_encode ( array ('code' => 99,'msg' => lang ( 'send_success' ) ) );
				else
					echo json_encode ( array ('code' => 1,'msg' => lang ( 'send_fail' ) ) );
			} else
				echo json_encode ( array ('code' => 98,'msg' => lang ( 'send_fail' ) ) );
		}
	}
	function index() {
		$type = $this->request->param ( 'type' );
		if ($this->request->isPost ()) {
			$code = $this->request->param ( 'code' );
			switch ($type) {
				case 'email' :
					$email = $this->request->param ( 'email' );
					$codeInfo = model ( 'Verifycode' )->getInfo ( array ([ 'account','=',$email ],[ 'type','=',$type ],[ 'code','=',$code ] ) );
					if (count ( $codeInfo ) == 0)
						die ( json_encode ( array ('code' => 1,'msg' => lang ( 'verifycode_error' ) ) ) );
					$info = model ( 'Member' )->getInfo ( array ([ 'email','=',$email ] ) );
					if (count ( $info ) > 0)
						die ( json_encode ( array ('code' => 1,'msg' => lang ( 'email_exist' ) ) ) );
					$data ['email'] = $email;
					$log = model ( 'Member' )->edit ( array ([ 'user_id','=',$this->uid ] ), $data );
					if ($log > 0)
						echo json_encode ( array ('code' => 99,'msg' => lang ( 'run_success' ) ) );
					else
						echo json_encode ( array ('code' => 97,'msg' => lang ( 'run_fail' ) ) );
					break;
				case 'pwd' :
					$pwd = $this->request->param ( 'pwd' );
					$newpwd = $this->request->param ( 'newpwd' );
                    $paypwd = $this->request->param ( 'paypwd' );
			        /*$codeInfo = model ( 'Verifycode' )->getInfo ( array ([ 'account','=',$this->member ['email'] ],[ 'type','=','email' ],[ 'code','=',$code ] ) );
					if (count ( $codeInfo ) == 0)
						die ( json_encode ( array ('code' => 1,'msg' => lang ( 'verifycode_error' ) ) ) );*/
					if ($pwd == '' || $pwd != $newpwd) {
                        echo json_encode(array('code' => 97, 'msg' => lang('password_error')));
                        break;
                    }
                    if ($paypwd != $this->member['paypwd'] ) {
                        echo json_encode(array('code' => 97, 'msg' => lang('paypwd_error')));
                        break;
                    }
                    $data ['password'] = $pwd;
                    $log = model ( 'Member' )->edit ( array ([ 'user_id','=',$this->uid ] ), $data );
                    if ($log > 0)
                        echo json_encode ( array ('code' => 99,'msg' => lang ( 'run_success' ) ) );
                    else
                        echo json_encode ( array ('code' => 97,'msg' => lang ( 'run_fail' ) ) );
					break;
					//////////////////////
					case 'edi_email' :
					$email = $this->request->param ( 'email' );
					$newemail = $this->request->param ( 'newemail' );
                    $paypwd = $this->request->param ( 'paypwd' );
			        /*$codeInfo = model ( 'Verifycode' )->getInfo ( array ([ 'account','=',$this->member ['email'] ],[ 'type','=','email' ],[ 'code','=',$code ] ) );
					if (count ( $codeInfo ) == 0)
						die ( json_encode ( array ('code' => 1,'msg' => lang ( 'verifycode_error' ) ) ) );*/
					if ($email == '' || $email != $newemail) {
                        echo json_encode(array('code' => 97, 'msg' => lang('password_error')));
                        break;
                    }
                    if ($paypwd != $this->member['paypwd'] ) {
                        echo json_encode(array('code' => 97, 'msg' => lang('paypwd_error')));
                        break;
                    }
                    $data ['email'] = $email;
                    $log = model ( 'Member' )->edit ( array ([ 'user_id','=',$this->uid ] ), $data );
                    if ($log > 0)
                        echo json_encode ( array ('code' => 99,'msg' => lang ( 'run_success' ) ) );
                    else
                        echo json_encode ( array ('code' => 97,'msg' => lang ( 'run_fail' ) ) );
					break;
					//////////////////////////////////////////////
				case 'paypwd' :
					$paypwd = $this->request->param ( 'paypwd' );
					$newpaypwd = $this->request->param ( 'newpaypwd' );
					$codeInfo = model ( 'Verifycode' )->getInfo ( array ([ 'account','=',$this->member ['email'] ],[ 'type','=','email' ],[ 'code','=',$code ] ) );
					if (count ( $codeInfo ) == 0)
						die ( json_encode ( array ('code' => 1,'msg' => lang ( 'verifycode_error' ) ) ) );
					if ($paypwd == '' || $paypwd != $newpaypwd)
						echo json_encode ( array ('code' => 97,'msg' => lang ( 'paypwd_error' ) ) );
					else {
						$data ['paypwd'] = $paypwd;
						$log = model ( 'Member' )->edit ( array ([ 'user_id','=',$this->uid ] ), $data );
						if ($log > 0)
							echo json_encode ( array ('code' => 99,'msg' => lang ( 'run_success' ) ) );
						else
							echo json_encode ( array ('code' => 97,'msg' => lang ( 'run_fail' ) ) );
					}
					break;
			}
		} else {
			$map [] = [ 'user_id','=',$this->uid ];
			$info = model ( 'Member' )->getInfo ( $map );
            if ($info ['email']) {
                $emailArr = explode('@', $info['email']);
                $emailhide = '';
                if (strlen($emailArr[0]) > 0) {
                    $emailhide = substr_replace($emailArr[0], '****', 3, 4);
                }
                if (count($emailArr) == 2) {
                    $emailhide .= '@'.$emailArr[1];
                }
                $info ['email'] = $emailhide;
            }
			$this->assign ( 'info', $info );
			return $this->fetch ( $type );
		}
	}
}
